﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace TCPTest
{
    class Game
    {
        Player player1, player2;
        Board board;
        NetworkStream stream;

        public Game(NetworkStream stream) 
        {
            this.stream = stream;
            board = new Board();

            setupGame();
            
            play();
        }

        private void setupGame()
        {
            string stringData = "";
            TCP.RespondToClient(stream, TCP.WELCOME);

            while (true)
            {
                stringData = TCP.GetNextLineFromClient(stream);
                if (stringData == "human-ai")
                {
                    player1 = new Human(SpaceColor.BLACK);
                    break;
                }
                else if (stringData.StartsWith("ai-ai"))
                {
                    player1 = new AIPlayer(AIDifficulty.MEDIUM, SpaceColor.BLACK);
                    player2 = new AIPlayer(AIDifficulty.MEDIUM, SpaceColor.WHITE);
                    return;
                }
                else
                    TCP.RespondToClient(stream, TCP.ILLEGAL);
            }
            TCP.RespondToClient(stream, TCP.OK);

            while (true)
            {
                stringData = TCP.GetNextLineFromClient(stream);
                if (stringData == "hard")
                {
                    player2 = new AIPlayer(AIDifficulty.HARD, SpaceColor.WHITE);
                    break;
                }
                else if (stringData == "medium")
                {
                    player2 = new AIPlayer(AIDifficulty.MEDIUM, SpaceColor.WHITE);
                    break;
                }
                else if (stringData == "easy")
                {
                    player2 = new AIPlayer(AIDifficulty.EASY, SpaceColor.WHITE);
                    break;
                }
                else
                    TCP.RespondToClient(stream, TCP.ILLEGAL);
            }
            TCP.RespondToClient(stream, TCP.OK);
        }        

        private void play()
        {
            int turn = 0;
            while (!board.gameEnd())
            {
                try
                {
                    if (isP1Turn(turn))
                    {
                        Move nextMove = player1.getNextMove(board, stream);
                        board.doMove(nextMove);

                        if (player1 is Human)
                            TCP.RespondToClient(stream, TCP.OK);
                        else
                        {
                            TCP.RespondToClient(stream, nextMove.ToString());
                            string acknowledgement = TCP.GetNextLineFromClient(stream);
                            if (acknowledgement != "OK")
                                throw new Exception("Did not get proper response from client");
                        }
                    }
                    else
                    {
                        Move nextMove = player2.getNextMove(board, stream);
                        board.doMove(nextMove);
                        if(player1 is Human)
                            TCP.RespondToClient(stream, board.ToString());
                        TCP.RespondToClient(stream, nextMove.ToString());
                        string acknowledgement = TCP.GetNextLineFromClient(stream);
                        if (acknowledgement != "OK")
                            throw new Exception("Did not get proper response from client");
                    }

                    turn++;
                }
                catch(Exception e)
                {
                    TCP.RespondToClient(stream, TCP.ILLEGAL);
                }
            }

            turn--;
            if (isP1Turn(turn))
                TCP.RespondToClient(stream, board.ToString());

            if (board.terminatingCondition(SpaceColor.WHITE))
                TCP.RespondToClient(stream, "Game Over - Player 2 Wins!\r\n");
            else
                TCP.RespondToClient(stream, "Game Over - Player 1 Wins!\r\n");

        }

        private bool isP1Turn(int turn)
        {
            return turn % 2 == 0;
        }

        public static void executeMove(Move move)
        {
            Console.WriteLine("Executing move: "+move.x + ", "+move.y);
        }

    }
}
